package cn.demarcia.coco.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.demarcia.coco.R;
import cn.demarcia.coco.ui.adapter.NewsAdapter;
import cn.demarcia.coco.uihelper.ToastUtils;
import info.jimao.newsdk.JuHeApi;
import info.jimao.newsdk.callbacks.SingleDataCallback;
import info.jimao.newsdk.models.NewsModel;
import info.jimao.newsdk.results.SingleDataResult;
import okhttp3.Call;


public class NewsViewPager extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
    public static final String TOP = "top";
    public static final String SOCIETY = "shehui";
    public static final String INNER = "guonei";
    public static final String INTERNATIONAL = "guoji";
    public static final String ENTERTAINMENT = "yule";
    public static final String PHYSICAL = "tiyu";
    public static final String MILITARY = "junshi";
    public static final String SCIENCE = "keji";
    public static final String FINANCE = "caijing";
    public static final String FASHION = "shishang";

    private RecyclerView.Adapter mAdapter;
    @Bind(R.id.recyclerViewHistory)
    RecyclerView mRecyclerView;
    @Bind(R.id.swipeRefreshLayout)
    SwipeRefreshLayout layout;

    private List<Object> mItems = new ArrayList<>();
    private String mType;
    private LinearLayoutManager mLinearLayoutManager;
    private JuHeApi mNewsApi;

    public static NewsViewPager getInstance(String type) {
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        NewsViewPager dynamicViewPager = new NewsViewPager();
        dynamicViewPager.setArguments(bundle);
        return dynamicViewPager;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        mNewsApi = new JuHeApi(getContext());
        mType = getArguments().getString("type");
        loadData(mType);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View pagerView = inflater.inflate(R.layout.base_list_view_pager, container, false);
        ButterKnife.bind(this, pagerView);

        mLinearLayoutManager = new LinearLayoutManager(getContext());
        mAdapter = new NewsAdapter(getContext(), mItems);
        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.addOnScrollListener(new MyOnScrollListener());
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());

        layout.setColorSchemeResources(R.color.colorPrimaryLight, R.color.colorPrimaryDark);
        layout.setProgressBackgroundColorSchemeColor(getResources().getColor(R.color.white));
        layout.setSize(SwipeRefreshLayout.DEFAULT);
        layout.setOnRefreshListener(this);
        layout.setRefreshing(true);
        return pagerView;
    }

    private void loadData(String type) {
        mNewsApi.getNews(type, new SingleDataCallback<NewsModel>(NewsModel.class) {
            @Override
            public void onError(Call call, Exception e) {
                super.onError(call, e);
                ToastUtils.show(getContext(), e.getMessage());
            }

            @Override
            public void onResponse(SingleDataResult<NewsModel> response) {
                super.onResponse(response);
                mItems.clear();
                mItems.addAll(response.getResult().data);
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onAfter() {
                super.onAfter();
                layout.setRefreshing(false);
            }
        });
    }

    @Override
    public void onRefresh() {
        layout.setRefreshing(true);
        loadData(mType);
    }


    //内部类，继承RecyclerView的滚动监听
    private class MyOnScrollListener extends RecyclerView.OnScrollListener {
        public MyOnScrollListener() {
            super();
        }

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            if (mItems.isEmpty()) {
                return;
            }
            int lastVisibleItem = mLinearLayoutManager.findLastVisibleItemPosition();
            int totalItemCount = mLinearLayoutManager.getItemCount();

           /* if (lastVisibleItem >= totalItemCount - 2 && dy > 0) {
                loadData(mType);
            }*/
        }
    }
}
